home *** CD-ROM | disk | FTP | other *** search
/ Developer Helper 1: Phil & Dave's Excellent CD / Excellent CD HFS.raw / Moof / Goodies / DTS Goodies / MDEF (LS Pascal) / Utilities Unit < prev   
Text File  |  2022-08-05  |  15KB  |  544 lines

  1. { ********************************************************** }
  2. {                                                            }
  3. {  Loose translation of ZoomRects() from an early release    }
  4. {    of the Apple Software Supplement.                       }
  5. {                                                            }
  6. { ********************************************************** }
  7.  
  8. unit utilities;
  9.  
  10. interface
  11.  
  12.     const
  13.         ZOOMSTEPS = 16;
  14.         ONE = 65536;
  15.         CurDirStorePtr = $0398;
  16.  
  17.     var
  18.         fract: Fixed;
  19.         CurDirStore: ^longint;
  20.  
  21.     function HFSRunning: boolean;
  22.     function NewRoms: boolean;
  23.     function blend (i1, i2: integer): integer;
  24.     procedure zoomrect (smallrect, bigrect: Rect; zoomup: boolean);
  25.     procedure ltog (var r: Rect);
  26.     procedure zoomport (wind: WindowPtr; up: Boolean);
  27.     procedure centerwindow (wind: WindowPtr; r: Rect);
  28.     procedure centerrect (var r1: Rect; r2: Rect);
  29.     function TrackMyRect (aPoint: Point; r1: Rect; rad1, rad2: integer): Boolean;
  30.     procedure DoAdjust (var r: Rect);
  31.     procedure DoShowDialog (Dptr: DialogPtr);
  32.     procedure get_resource_error (rtype: Str255; id: integer);
  33.     procedure show_io_err (err: OSErr);
  34.  
  35.  
  36. implementation
  37.  
  38.     function HFSRunning;
  39.  
  40.         const
  41.             FSFCBLen = $3F6;
  42.  
  43.         var
  44.             HFS: ^integer;
  45.  
  46.     begin
  47.         HFS := pointer(FSFCBLen);
  48.         HFSRunning := (HFS^ > 0);
  49.     end;
  50.  
  51.     function NewRoms;
  52.  
  53.         const
  54.             NewRomsID = 117;
  55.  
  56.         var
  57.             RomVersion, Machine: integer;
  58.  
  59.     begin
  60.         Environs(RomVersion, Machine);
  61.         NewRoms := RomVersion >= NewRomsID;
  62.     end;
  63.  
  64.     function blend;
  65.  
  66.         var
  67.             smallFix, bigFix, tempFix: Fixed;
  68.  
  69.     begin
  70.         smallFix := ONE * i1;
  71.         bigFix := ONE * i2;
  72.         tempFix := FixMul(fract, bigFix) + FixMul(ONE - fract, smallFix);
  73.         blend := FixRound(tempFix);
  74.     end;
  75.  
  76.     procedure zoomrect;
  77.  
  78.         var
  79.             factor: Fixed;
  80.             rect1, rect2, rect3, rect4: Rect;
  81.             savePort, deskPort: GrafPtr;
  82.             i: integer;
  83.  
  84.     begin
  85.         GetPort(savePort);
  86.         deskPort := GrafPtr(NewPtr(sizeof(GrafPort)));
  87.         OpenPort(deskPort);
  88.         InitPort(deskPort);
  89.         SetPort(deskPort);
  90.         PenPat(gray);
  91.         PenMode(notPatXor);
  92.         if zoomup then
  93.             begin
  94.                 rect1 := smallrect;
  95.                 factor := FixRatio(6, 5);
  96.                 fract := FixRatio(541, 10000);
  97.             end
  98.         else
  99.             begin
  100.                 rect1 := bigrect;
  101.                 factor := FixRatio(5, 6);
  102.                 fract := ONE;
  103.             end;
  104.         rect2 := rect1;
  105.         rect3 := rect1;
  106.         FrameRect(rect1);
  107.         for i := 1 to ZOOMSTEPS do
  108.             begin
  109.                 rect4.left := blend(smallrect.left, bigrect.left);
  110.                 rect4.right := blend(smallrect.right, bigrect.right);
  111.                 rect4.top := blend(smallrect.top, bigrect.top);
  112.                 rect4.bottom := blend(smallrect.bottom, bigrect.bottom);
  113.                 FrameRect(rect4);
  114.                 FrameRect(rect1);
  115.                 rect1 := rect2;
  116.                 rect2 := rect3;
  117.                 rect3 := rect4;
  118.                 fract := FixMul(fract, factor);
  119.             end;
  120.         FrameRect(rect1);
  121.         FrameRect(rect2);
  122.         FrameRect(rect3);
  123.         ClosePort(deskPort);
  124.         DisposPtr(Ptr(deskPort));
  125.         PenNormal;
  126.         SetPort(savePort);
  127.     end;
  128.  
  129. { ********************************************************** }
  130. {                                                            }
  131. {       procedure ltog(r : Rect);                            }
  132. {                                                            }
  133. {  Converts the Rect referenced by r from local to global    }
  134. {  coordinate system.                                        }
  135. {                                                            }
  136. { ********************************************************** }
  137.  
  138.  
  139.     procedure ltog;
  140.  
  141.         var
  142.             p1, p2: Point;
  143.  
  144.     begin
  145.         p1 := r.topLeft;
  146.         p2 := r.botRight;
  147.         LocalToGlobal(p1);
  148.         LocalToGlobal(p2);
  149.         Pt2Rect(p1, p2, r);
  150.     end;
  151.  
  152. { ********************************************************** }
  153. {                                                            }
  154. {     procedure zoomport(wind:WindowPtr;up:Boolean);         }
  155. {                                                            }
  156. {   Zooms the window referenced by "wind" either from an     }
  157. {   inivisible state to a visible state, or vice versa. Pass }
  158. {   TRUE in the "up" Boolean parameter to zoom a window to   }
  159. {   open, an FALSE to zoom it close.  The WindowPtr must     }
  160. {   have already been created elsewhere, and zooming the     }
  161. {   window invisible only hides the window, it does not      }
  162. {   destroy the WindowPtr data.                              }
  163. {                                                            }
  164. { ********************************************************** }
  165.  
  166.     procedure zoomport;
  167.  
  168.         var
  169.             r1, r2, r3: Rect;
  170.  
  171.     begin
  172.         SetPort(wind);
  173.         SetRect(r1, 0, 20, 0, 20);
  174.         r3 := wind^.portRect;
  175.         r2 := r3;
  176.         InsetRect(r2, (r3.right - r3.left + 20) div 2, (r3.bottom - r3.top + 20) div 2);
  177.  
  178.         ltog(r2);
  179.         ltog(r3);
  180.  
  181.         if up then
  182.             begin
  183.                 zoomrect(r1, r2, TRUE);
  184.                 zoomrect(r2, r3, TRUE);
  185.                 ShowWindow(wind);
  186.                 SetPort(wind);
  187.             end
  188.         else
  189.             begin
  190.                 HideWindow(wind);
  191.                 zoomrect(r2, r3, FALSE);
  192.                 zoomrect(r1, r2, FALSE);
  193.             end;
  194.     end;
  195.  
  196. {********************************************************************}
  197. {                                                                    }
  198. {     procedure centerwindow(wind:WindowPtr;r:Rect);                 }
  199. {                                                                    }
  200. {   centers the window referenced by the WindowPtr: wind within      }
  201. {   the Rect referenced by the Rect* r.  To center a window in       }
  202. {   the Macintosh screen, (or primary screen if using a Mac II),     }
  203. {   call...                                                          }
  204. {                                                                    }
  205. {     centerwindow(theWindow,&screenBits.bounds);                    }
  206. {                                                                    }
  207. {********************************************************************}
  208.  
  209.     procedure centerwindow;
  210.  
  211.         var
  212.             r2: Rect;
  213.             windW, windH: integer;
  214.             rectW, rectH: integer;
  215.             newW, newH: integer;
  216.  
  217.     begin
  218.         r2 := wind^.portRect;
  219.         windW := r2.right - r2.left;
  220.         windH := r2.bottom - r2.top;
  221.         rectW := r.right - r.left;
  222.         rectH := r.bottom - r.top;
  223.         newW := r.left + (rectW - windW) div 2;
  224.         newH := r.top + (rectH - windH) div 2;
  225.         MoveWindow(wind, newW, newH, FALSE);
  226.     end;
  227.  
  228. {********************************************************************}
  229. {                                                                    }
  230. {        procedure centerrect(r1,r2:Rect);                           }
  231. {                                                                    }
  232. {   centers the rectangle referenced by the Rect* r1 within          }
  233. {   the Rect referenced by the Rect* r2.  To center the Rect         }
  234. {   innerRect within the Rect outerRect, call...                     }
  235. {                                                                    }
  236. {        centerrect(&innerRect,&outerRect);                          }
  237. {                                                                    }
  238. {********************************************************************}
  239.  
  240.     procedure centerrect;
  241.  
  242.     begin
  243.         OffsetRect(r1, ((r2.right - r2.left) - (r1.right - r1.left)) div 2 - r1.left, ((r2.bottom - r2.top) - (r1.bottom - r1.top)) div 2 - r1.top);
  244.     end;
  245.  
  246.  
  247. {********************************************************************}
  248. {                                                                    }
  249. { Function TrackMyRect(aPoint:Point;r1:Rect;rad1,rad2:integer):Boolean; }
  250. {                                                                    }
  251. {   TrackMyRect() treats the Rect referenced by *r1 much like        }
  252. {   the TrackControl(ControlHandle) of the Control Manager. The      }
  253. {   point passed by aPoint should be the local coordinates of the    }
  254. {   mouse down location in the window in which the Rect resides,     }
  255. {   and should initially be called with the mouse location inside    }
  256. {   of the Rect.                                                     }
  257. {                                                                    }
  258. {   rad1 and rad2 are radii for rounded rects, pass 0 in these       }
  259. {   values if the Rect is not rounded.                               }
  260. {                                                                    }
  261. {   Sample Code Fragment....                                         }
  262. {                                                                    }
  263. {   thePoint     : Point;                                            }
  264. {   myRect       : Rect;                                             }
  265. {   rectSelected : Boolean;                                          }
  266. {                                                                    }
  267. {   thePoint := theEvent.where;                                      }
  268. {   GlobalToLocal(&thePoint);                                        }
  269. {   if (PtInRect(thePoint,&myRect))                                  }
  270. {       rectSelected := TrackMyRect(thePoint,&myRect,0,0);           }
  271. {                                                                    }
  272. {********************************************************************}
  273.  
  274.     function TrackMyRect;
  275.  
  276.         var
  277.             returnVal: boolean;
  278.  
  279.     begin
  280.         returnVal := TRUE;
  281.         InvertRoundRect(r1, rad1, rad2);
  282.         repeat
  283.             begin
  284.                 GetMouse(aPoint);
  285.                 if (PtInRect(aPoint, r1) <> returnVal) then
  286.                     begin
  287.                         returnVal := not returnVal;
  288.                         InvertRoundRect(r1, rad1, rad2);
  289.                         SystemTask;
  290.                     end;
  291.             end;
  292.         until StillDown;
  293.         GetMouse(aPoint);
  294.         TrackMyRect := PtInRect(aPoint, r1);
  295.     end;
  296.  
  297.     procedure DoAdjust;
  298.  
  299.         var
  300.             x, y, xd, yd: LongInt;
  301.  
  302.     begin
  303.         if not ((screenBits.bounds.right = 512) and (screenBits.bounds.bottom = 342)) then
  304.             begin
  305.                 xd := (r.left - r.right) div 2;
  306.                 yd := (r.bottom - r.top) div 2;
  307.                 x := (((r.right + xd) * screenBits.bounds.right) div 512) - xd - r.right;
  308.                 y := (((r.top + yd) * screenBits.bounds.bottom) div 342) - yd - r.top;
  309.                 OffsetRect(r, x, y);
  310.             end;
  311.     end;
  312.  
  313.     procedure DoShowDialog;
  314.  
  315.         var
  316.             r1, r2, r: Rect;
  317.  
  318.     begin
  319.         r1 := Dptr^.portBits.bounds;
  320.         r2 := Dptr^.portRect;
  321.         r.top := -1 * r1.top;    {-1.r1.top }
  322.         r.left := -1 * r1.left;
  323.         r.right := (r2.right - r2.left) + r.left;
  324.         r.bottom := (r2.bottom - r2.top) + r.top;
  325.         DoAdjust(r);
  326.         MoveWindow(Dptr, r.left, r.top, True);
  327.         ShowWindow(Dptr);
  328.     end;
  329.  
  330.     procedure get_resource_error;
  331.  
  332.         const
  333.             p0 = 'The program had trouble loading a resource, the resource is:';       {static text}
  334.  
  335.         var
  336.             myDialog: DialogPtr;
  337.             myDialogPeek: DialogPeek;
  338.             dStorage: DialogRecord;
  339.             itemNumber: integer;
  340.             itemType: integer;
  341.             itemList, itemHandle: Handle;
  342.             dispRect, dRect: Rect;
  343.             theString: str255;
  344.             HMT, zMessage, zAddress: str255;
  345.  
  346.         procedure DefineDialog (var myDialog: DialogPtr;        {create Dialog in memory}
  347.                                         var dStorage: DialogRecord);
  348.  
  349.             const
  350.                 statTextLength = 2;
  351.                 statTextNu = 2;
  352.  
  353.             type        {these are for creating the Dialog Template in memory}
  354.                 TextT = packed array[1..statTextLength] of char;
  355.                 StatTextTitleT = array[1..statTextNu] of string[statTextLength];
  356.                 StatTextRectT = array[1..statTextNu] of Rect;
  357.                 ButtonsType = record
  358.                         CtlHndl: Handle;
  359.                         Itemrect: Rect;
  360.                         ItemType, ItemLen: SignedByte;
  361.                         zTitle: packed array[1..4] of char;
  362.                     end;
  363.                 StatTextsType = array[1..statTextNu] of record
  364.                         statTextHndl: Handle;
  365.                         Itemrect: Rect;
  366.                         ItemType, ItemLen: SignedByte;
  367.                         zText: TextT;
  368.                     end;
  369.                 ItemListT = record
  370.                         ItemCountM1: integer;
  371.                         myButtons: ButtonsType;
  372.                         myStatTexts: StatTextsType;
  373.                     end;
  374.                 ItemListTPtr = ^ItemListT;
  375.                 ItemListTHdl = ^ItemListTPtr;
  376.  
  377.             var
  378.                 DITLHdl: ItemListTHdl;
  379.                 frameRect: Rect;
  380.                 zSTTitle: StatTextTitleT;
  381.                 zSTRect: StatTextRectT;
  382.                 j: integer;
  383.  
  384.         begin
  385.             SetRect(zSTRect[1], 10, 5, 300, 40);
  386.             SetRect(zSTRect[2], 10, 60, 300, 110);
  387.             DITLHdl := ItemListTHdl(NewHandle(SizeOf(ItemListT)));      {create the DialogTemplate}
  388.             HLock(handle(DITLHdl));
  389.             with DITLHdl^^ do
  390.                 begin
  391.                     ItemCountM1 := 1 + statTextNu - 1;
  392.                     with myButtons do
  393.                         begin
  394.                             CtlHndl := nil;
  395.                             SetRect(Itemrect, 210, 100, 300, 118);
  396.                             frameRect := Itemrect;
  397.                             ItemType := CtrlItem + BtnCtrl;
  398.                             ItemLen := 4;
  399.                             zTitle := ' OK ';
  400.                         end;
  401.                     for j := 1 to statTextNu do
  402.                         with myStatTexts[j] do
  403.                             begin
  404.                                 statTextHndl := nil;
  405.                                 Itemrect := zSTRect[j];
  406.                                 ItemType := statText;
  407.                                 ItemLen := statTextLength;
  408.                                 NumToString(j - 1, theString);
  409.                                 zText[1] := '^';
  410.                                 zText[2] := theString[1];
  411.                             end;
  412.                 end;
  413.             HUnLock(handle(DITLHdl));
  414.             itemList := Handle(DITLHdl);
  415.         end;
  416.  
  417.     begin
  418.         DefineDialog(myDialog, dStorage);
  419.         FlushEvents(everyEvent, 0);
  420.         InitCursor;
  421.         SetRect(dRect, 0, 0, 310, 125);
  422.         centerrect(dRect, screenBits.bounds);
  423.         NumToString(id, theString);
  424.         zAddress := Concat('Type: ', rtype, '    Number: ', theString);
  425.         ParamText(p0, zAddress, '', '');
  426.         myDialog := NewDialog(@dStorage, dRect, '', TRUE, DBoxProc, WindowPtr(-1), FALSE, 0, itemList);
  427.         repeat
  428.             ModalDialog(nil, itemNumber);
  429.         until itemNumber = OK;
  430.         myDialogPeek := DialogPeek(myDialog);
  431.         CloseDialog(myDialog);
  432.         DisposHandle(myDialogPeek^.items);
  433.     end;
  434.  
  435.     procedure show_io_err;
  436.  
  437.         const
  438.             err_alert_id = 256;
  439.             io_err_string_id = 257;
  440.             fsDSIntErr = -127;
  441.  
  442.         var
  443.             err_string: Str255;
  444.             offset, alert_result: integer;
  445.  
  446.     begin
  447.         case err of
  448.             badMDBErr: 
  449.                 offset := 1;
  450.             badMovErr: 
  451.                 offset := 2;
  452.             bdNamErr: 
  453.                 offset := 3;
  454.             dirFulErr: 
  455.                 offset := 4;
  456.             dirNFErr: 
  457.                 offset := 5;
  458.             dskFulErr: 
  459.                 offset := 6;
  460.             dupFNErr: 
  461.                 offset := 7;
  462.             eofErr: 
  463.                 offset := 8;
  464.             extFSErr: 
  465.                 offset := 9;
  466.             fBsyErr: 
  467.                 offset := 10;
  468.             fLckdErr: 
  469.                 offset := 11;
  470.             fnfErr: 
  471.                 offset := 12;
  472.             fnOpnErr: 
  473.                 offset := 13;
  474.             fsDSIntErr: 
  475.                 offset := 14;
  476.             fsRnErr: 
  477.                 offset := 15;
  478.             gfpErr: 
  479.                 offset := 16;
  480.             ioErr: 
  481.                 offset := 17;
  482.             memFullErr: 
  483.                 offset := 18;
  484.             noMacDskErr: 
  485.                 offset := 19;
  486.             nsDrvErr: 
  487.                 offset := 20;
  488.             nsvErr: 
  489.                 offset := 21;
  490.             opWrErr: 
  491.                 offset := 22;
  492.             paramErr: 
  493.                 offset := 23;
  494.             permErr: 
  495.                 offset := 24;
  496.             posErr: 
  497.                 offset := 25;
  498.             rfNumErr: 
  499.                 offset := 26;
  500.             tmfoErr: 
  501.                 offset := 27;
  502.             tmwdoErr: 
  503.                 offset := 28;
  504.             volOffLinErr: 
  505.                 offset := 29;
  506.             volOnLinErr: 
  507.                 offset := 30;
  508.             vLckdErr: 
  509.                 offset := 31;
  510.             wrgVolTypErr: 
  511.                 offset := 32;
  512.             wrPermErr: 
  513.                 offset := 33;
  514.             wPrErr: 
  515.                 offset := 34;
  516.  
  517.             resNotFound: 
  518.                 offset := 35;
  519.             resFNotFound: 
  520.                 offset := 36;
  521.             addResFailed: 
  522.                 offset := 37;
  523.             rmvResFailed: 
  524.                 offset := 38;
  525.             resAttrErr: 
  526.                 offset := 39;
  527.             mapReadErr: 
  528.                 offset := 40;
  529.  
  530.             otherwise
  531.                 ;
  532.         end;
  533.         GetIndString(err_string, io_err_string_id, offset);
  534.         if err_string = '' then
  535.             begin
  536.                 get_resource_error('STR#', io_err_string_id);
  537.                 ExitToShell;
  538.             end;
  539.         ParamText(err_string, '', '', '');
  540.         alert_result := Alert(err_alert_id, nil);
  541.         ExitToShell;
  542.     end;
  543.  
  544. end.